﻿#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QSurfaceFormat>

#include "dialog.h"

static Dialog * g_mainDlg = Q_NULLPTR;

static QtMsgType g_msgType = QtInfoMsg;
static QtMessageHandler g_oldMessageHandler = Q_NULLPTR;

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);

int main(int argc, char *argv[])
{
#ifdef Q_OS_WIN32
    qputenv("QTSCRCPY_ADB_PATH", "F:\\platform-tools\\adb.exe");
#endif

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    g_oldMessageHandler = qInstallMessageHandler(myMessageOutput);
    QApplication a(argc, argv);
    a.setApplicationVersion("V1.0.1");
    qDebug()<<a.applicationVersion();
    qDebug()<<a.applicationName();

    qDebug()<<QCoreApplication::applicationVersion();
    QStringList versionList = QCoreApplication::applicationVersion().split(".");
    if (versionList.size() >= 3) {
        QString version = versionList[0] + "." + versionList[1] + "." + versionList[2];
        a.setApplicationVersion(version);
    }

    g_mainDlg = new Dialog {};
    g_mainDlg->show();

    int ret = a.exec();
    delete g_mainDlg;

    return ret;
}

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    if (g_oldMessageHandler) {
        g_oldMessageHandler(type, context, msg);
    }

    // Is Qt log level higher than warning?
    float fLogLevel = g_msgType;
    if (QtInfoMsg == g_msgType) {
        fLogLevel = QtDebugMsg + 0.5f;
    }
    float fLogLevel2 = type;
    if (QtInfoMsg == type) {
        fLogLevel2 = QtDebugMsg + 0.5f;
    }

    if (fLogLevel <= fLogLevel2) {
        if (g_mainDlg && g_mainDlg->isVisible() && !g_mainDlg->filterLog(msg)) {
            g_mainDlg->outLog(msg);
        }
    }

    if (QtFatalMsg == type) {
        //abort();
    }
}
